package com.dqx.opengl_es_demo.face;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;

import java.util.List;

/**
 * 摄像头工具类
 */
public class CameraUtils {
    private Context context;
    //摄像头
    protected Camera mCamera;
    //摄像头信息
    protected Camera.CameraInfo mCameraInfo;
    //预览的宽度
    public static final int PREVIEW_WIDTH = 320;
    //预览的高度
    public static final int PREVIEW_HEIGHT = 240;
    //标记摄像头是否是前置摄像头
    private int CameraFacing = Camera.CameraInfo.CAMERA_FACING_FRONT;
    //预览回调
    private Camera.PreviewCallback mPreviewCallback;

    public CameraUtils(Context context) {
        this.context = context;
    }

    /**
     * 打开摄像头
     *
     * @param surfaceTexture 纹理数据
     */
    public void openCamera(SurfaceTexture surfaceTexture) {
        //释放之前打开摄像头资源
        if (null != mCamera) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
        //打开前置摄像头
        Camera.CameraInfo info = new Camera.CameraInfo();
        for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraFacing) {
                try {
                    //打开摄像头
                    mCamera = Camera.open(i);
                    mCameraInfo = info;
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    mCamera = null;
                    continue;
                }
                break;
            }
        }
        try {
            //设置预览
            mCamera.setPreviewTexture(surfaceTexture);
            initCamera();
        } catch (Exception ex) {
            if (null != mCamera) {
                mCamera.release();
                mCamera = null;
            }
        }
    }

    /**
     * 初始化摄像头相关参数
     */
    private void initCamera() {
        if (null != mCamera) {
            try {
                Camera.Parameters parameters = mCamera.getParameters();
                List<String> flashModes = parameters.getSupportedFlashModes();
                //关闭闪光灯
                if (flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_OFF)) {
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                }
                //获取摄像头支持的相片大小
                List<Camera.Size> pictureSizes = mCamera.getParameters()
                        .getSupportedPictureSizes();
                //设置预览大小
                parameters.setPreviewSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);
                //设置图片大小
                Camera.Size fs = null;
                for (int i = 0; i < pictureSizes.size(); i++) {
                    Camera.Size psize = pictureSizes.get(i);
                    if (fs == null && psize.width >= 1280)
                        fs = psize;
                }
                //设置照片大小
                parameters.setPictureSize(fs.width, fs.height);
                //设置摄像方向
                if (context.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {//竖屏
                    int orientation = CameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT ?
                            360 - mCameraInfo.orientation : mCameraInfo.orientation;
                    mCamera.setDisplayOrientation(orientation);
                } else {//横屏
                    mCamera.setDisplayOrientation(0);
                }
                //后置摄像头
                if (CameraFacing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {//是否有触摸对焦功能
                        //触摸聚焦
                        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
                    } else {
                        //自动聚焦
                        parameters.setFocusMode(parameters.FOCUS_MODE_AUTO);
                    }
                }
                //设置参数
                mCamera.setParameters(parameters);
                //设置预览回调
                mCamera.setPreviewCallback(this.mPreviewCallback);
                //开始执行预览
                mCamera.startPreview();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 设置预览回调
     *
     * @param previewCallback
     */
    public void setPreviewCallback(Camera.PreviewCallback previewCallback) {
        this.mPreviewCallback = previewCallback;
        if (mCamera != null) {
            mCamera.setPreviewCallback(previewCallback);
        }
    }

    /**
     * 获取摄像方向
     *
     * @return
     */
    public int getOrientation() {
        if (mCameraInfo != null) {
            return mCameraInfo.orientation;
        }
        return 0;
    }

    /**
     * 资源释放
     */
    public void release() {
        if (null != mCamera) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }

}
